<?php

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

// Load the view framework
jimport( 'joomla.application.component.view');
if(!class_exists('VirtueMartCart')) require(JPATH_COMPONENT.DS.'helpers'.DS.'cart.php');
if(!class_exists('calculationHelper')) require(JPATH_COMPONENT.DS.'helpers'.DS.'calculationh.php');

			
/**
* View for the shopping cart
* @package VirtueMart
* @author Max Milbers
* @author Oscar van Eijk
*/
class MvmViewCart extends JView {
	public function display($tpl = null) {
		$_currentUser =& JFactory::getUser();
		$this->_cart = VirtueMartCart::getCart(false);

		/* checkout or to confirm */
		if($this->_cart->getDataValidated()){
			$text = JText::_('COM_VIRTUEMART_ORDER_CONFIRM_MNU');
			$checkout_task = 'confirm';
		} else {
			$text = JText::_('COM_VIRTUEMART_CHECKOUT_TITLE');
			$checkout_task = 'checkout';
		}
		$this->assignRef('checkout_task', $checkout_task);
		$this->assignRef('userName', $_currentUser->username);
		
		$this->prepareUserData();
		$this->prepareCartData();
		$this->prepareCarriers();

		parent::display($tpl);
	}
	
	public function renderMail ($doVendor=false) {
		$tpl = ($doVendor) ? 'mail_html_vendor' : 'mail_html_shopper';
		if(!class_exists('VirtueMartCart')) require(JPATH_COMPONENT.DS.'helpers'.DS.'cart.php');

		$this->_cart = VirtueMartCart::getCart(false);
		$this->assignRef('cart', $this->_cart);
		$this->assignRef('lists', $this->lists);
		$this->prepareCartData();
		$this->prepareUserData();
		//$this->prepareAddressDataInCart();
	//	$this->prepareMailData();

		$this->subject = (true) ? JText::sprintf('COM_VIRTUEMART_NEW_ORDER_CONFIRMED',	$this->shopperName, $this->order['details']['BT']->order_total, $this->order['details']['BT']->order_number) : JText::sprintf('COM_VIRTUEMART_NEW_ORDER_CONFIRMED', $this->vendor->vendor_store_name, $this->order['details']['BT']->order_total, $this->order['details']['BT']->order_number, $this->order['details']['BT']->order_pass);

		$this->doVendor = true;
		$vendorModel = $this->getModel('vendor');
		$this->vendorEmail = $vendorModel->getVendorEmail(1) ; //$this->vendor->vendor_id);
		$this->layoutName = $tpl;
		$this->setLayout($tpl);
		parent::display();
	}
	

	/* private section goes here */
	
	private function prepareCarriers() {
		$_db = &JFactory::getDBO();
			$_q = 'SELECT v.`shipping_carrier_id`   AS id '
			. ',      v.`shipping_carrier_name` AS name '
			. 'FROM   #__vm_shipping_carrier AS v ';


		$_db->setQuery($_q);
		if (!$_res =  $_db->loadAssocList()) {
//			$app = JFactory::getApplication();
//			$app->enqueueMessage(JText::_('COM_VIRTUEMART_CART_NO_CARRIER'));
			return false;
		}
		foreach ($_res as $_r){
			$this->carrier[$_r['id']] = $_r['name'];
		}
		$this->assignRef('carriers', $this->carrier);
		
	}

	private function prepareUserData(){

		//For User address
		$_currentUser =& JFactory::getUser();
		$this->lists['current_id'] = $_currentUser->get('id');
//		$this->assignRef('virtuemart_user_id', $this->lists['current_id']);
		if($this->lists['current_id']){
			$this->_user = $this->getModel('user', 'ViruteMart');
			$this->_user->setCurrent();
			if(!$this->_user){

			}else{
				$this->assignRef('user', $this->_user);

				$this->_userDetails = $this->_user->getUser();

				//This are other contact details, like used in CB or so.
	//			$_contactDetails = $this->_user->getContactDetails();

				$this->assignRef('userDetails', $this->_userDetails);
			}
		}
	}

	private function prepareCartData(){

		/* Get the products for the cart */
		$prepareCartData = $this->_cart->prepareCartData();

		$this->assignRef('prices', $prepareCartData->prices);

		$this->assignRef('cartData',$prepareCartData->cartData);
		$this->assignRef('calculator',$prepareCartData->calculator);

	}
	
	private function prepareMailData(){

		if(empty($this->vendor)) $this->prepareVendor();
		//TODO add orders, for the orderId
		//TODO add registering userdata
		// In general we need for every mail the shopperdata (with group), the vendor data, shopperemail, shopperusername, and so on
	}
	
	private function prepareAddressDataInCart(){


		$userFieldsModel = $this->getModel('userfields', 'VirtuemartModel');

		$BTaddress['fields']= array();
		if(!empty($this->_cart->BT)){
			$userFieldsBT = $userFieldsModel->getUserFieldsFor('cart','BT');
			$userAddressData = $this->_cart->getCartAdressData('BT');
			$BTaddress = $userFieldsModel->getUserFieldsByUser(
							 $userFieldsBT
							,$userAddressData
							);

		}

		$this->assignRef('BTaddress',$BTaddress['fields']);

		$STaddress['fields']= array();
		if(!empty($this->_cart->ST)){
			$userFieldsST = $userFieldsModel->getUserFieldsFor('cart','ST');
			$userAddressData = $this->_cart->getCartAdressData('ST');
			$STaddress = $userFieldsModel->getUserFieldsByUser(
							 $userFieldsST
							,$userAddressData
							);

		}
		$this->assignRef('STaddress',$STaddress['fields']);

	}
		
}
